package com.sdry.web.controller.cb;

import com.sdry.mapper.cb.IDicMapper;
import com.sdry.model.cb.DicEntity;
import com.sdry.model.cb.TableDataInfo;
import com.sdry.service.lz.WarehouseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.applet.AppletContext;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: HYWLV_1.0
 * @description: 通用数据处理
 * @author: cb
 * @create: 2019-09-14 21:20
 **/
public class BaseController implements Serializable {

    @Autowired
    protected WarehouseService warehouseService;

    @Autowired static IDicMapper iDicMapper;


    public static Logger logger = LogManager.getLogger(ReportFormController.class);

    public static Map<String,Object> map = Collections.synchronizedMap(new HashMap<>());

    //静态方法启动只加载一次，查询出所有的
    static {
        //获取应用上下文对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
        iDicMapper = ctx.getBean(IDicMapper.class);
        queryDic();
    }

    protected static List<DicEntity> dicEntities = new ArrayList<>();
    protected static List<DicEntity> dicEntities2 = new ArrayList<>();
    private static void queryDic() {
        dicEntities = iDicMapper.queryAll();

    }

    /**
     * 返回的结果集
     * @param list 集合
     * @param total 总数
     * @return
     */
    public TableDataInfo getDataTable(List<?> list,Integer total) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setTotal(total);
        rspData.setCode(0);
        if (list.size() == 0 || list == null) {
            rspData.setRows(new ArrayList<>());
        }else {
            rspData.setRows(list);
        }
        return rspData;
    }

    public static List<DicEntity> getFieldDetail(String tableName){
        for (DicEntity dicEntity : dicEntities) {
            if (StringUtils.equals(dicEntity.getDictType(),tableName)) {
                dicEntities2.add(dicEntity);
            }
        }
        return dicEntities2;
    }



/*
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }*/
}
